<?php
namespace Home\Controller;
use Think\Controller;
class ShopingController extends Controller {
	public function __construct(){
		parent::__construct();
		if(!$_SESSION['user_is_login']){
			$this->error('没有登录',U('home/user/login'));
		}
	}	
	public function add(){
		 $prodect_id=I('post.prodect_id');
    	 $num=I('post.num');
    	 $user_id=$_SESSION['user_id'];
    	 if(!$user_cart=M('user_cart')->where(array(
    	 		'prodect_id'=>$prodect_id,
    	 		'user_id'=>$user_id,
    	 	))->find()){
			M('user_cart')->add(array(
    	 		'prodect_id'=>$prodect_id,
    	 		'user_id'=>$user_id,
                'num'=>$num
    	 	));
		}else{
			M('user_cart')->where(array(
    	 		'prodect_id'=>$prodect_id,
    	 		'user_id'=>$user_id,
    	 	))->save(array(
    	 		'num'=>$user_cart['num']+$num
    	 	));
		};
		$this->success('添加成功',U('home/prodect/detail/id/'.$prodect_id));	 
	}
     public function cart(){
        $user_id=$_SESSION['user_id'];
        $cart=M('user_cart')->where('user_id='.$user_id)->select();
        $this->assign('cart',$cart);
        $this->display();
    }
    public function update(){
        $num=I('post.num');
        $id=I('post.id');
        if($num>0){
        M('user_cart')->where('id='.$id)->save(array('num'=>$num));
        echo 'ok';}else{
            echo "商品件数不能少于1";
        }
    }
    public function del($id){
        M('user_cart')->where('id='.$id)->delete();
    }
    public function del_all(){
        $ids=I('post.ids');
        M('user_cart')->delete(implode(',', $ids));
        echo 'ok';
    }
    public function preorder(){
        $data=I('post.data');
        if(count($data)==0){
            echo "请至少选中一件商品";
        }else{
            $_SESSION['data']=$data;
            echo "ok";
        }
    }
    public function createorder(){
        $user_id=$_SESSION['user_id'];
        $address=M('user_addres')->where('user_id='.$user_id)->select();
        $data=$_SESSION['data'];
        $this->assign('address',$address);
        $this->assign('data',$data);
        $this->display();
    }
    public function do_createorder(){
        $address_id = I('post.address_id');
        if(!$address_id){
            $this->error('请选择一个收货地址');
        }
        $user=M('user')->where('id='.$_SESSION['user_id'])->find();
        $address=M('user_addres')->where('user_id='.$_SESSION['user_id'])->find();
        $order_id=M("orders")->add(array(
            'user_id'=>$user['id'],
            'username'=>$user['username'],
            'address_name'=>$address['name'],
            'address_phone'=>$address['phone'],
            'address_address'=>$address['address'],
            'address_id'=>$address_id,
            'creat_time'=>time(),
            'status'=>0
        ));
        $prodect_id = I('post.prodect_id');
        $num = I('post.num');
        $count = count($prodect_id);
        $total=0;
        for($i=0;$i<$count;$i++){
            $prodect=M('prodect')->where('id='.$prodect_id[$i])->find();
            $total +=floatval($prodect['price'])*intval($num[$i]);
            M("order_prodect")->add(array(
                'prodect_title'=>$prodect['title'],
                'prodect_price'=>$prodect['price'],
                'prodect_catalog_id'=>$prodect['catalog_id'],
                'order_id'=>$order_id['id'],
                'prodect_id'=>$prodect_id[$i],
                'num'=>$num[$i],
            ));
        }
        M("orders")->where('id='.$order_id['id'])->save(array(
                "total"=>$total,
            ));
        $this->success('订单提交成功！',U('home/front/index'));
    }
    public function saveaddress(){
        $name=I('post.name');
        $address=I('post.address');
        $phone=I('post.phone');
        if(empty($name)){
            echo json_encode(array('code'=>'error','field'=>'name','msg'=>'名字不能为空'));
            exit();
        };
        if(empty($address)){
            echo json_encode(array('code'=>'error','field'=>'address','msg'=>'地址不能为空'));
            exit();
        };
        if(empty($phone)){
            echo json_encode(array('code'=>'error','field'=>'phone','msg'=>'号码不能为空'));
            exit();
        };
        $user_id=$_SESSION['user_id'];
        M('user_addres')->add(array(
                'name'=>$name,
                'address'=>$address,
                'phone'=>$phone,
                'user_id'=>$user_id
            ));
        echo json_encode(array('code'=>'ok'));
    }
}